package com.wmx.yuanyuan.controller;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * * @Scope 指定实例范围：singleton、prototype、request、session、globalSession
 * 同理也可以用于其它的 @Controller、@Service、@Repository、@Component、@Bean 等
 *
 * @author wangMaoXiong
 */
@RestController
@RequestMapping("sys")
@Scope(value = "singleton")
public class SystemController implements SmartInitializingSingleton {

    private static final Logger LOG = LoggerFactory.getLogger(SystemController.class);

    private int id = 1;
    private static long count = 1;

    /**
     * singleton 单例时，应用一启动，就会默认执行无参构造器创建实例，放到容器中。
     * prototype 多例时，则是到获取实例的时候，才进行创建。
     */
    public SystemController() {
        System.out.println("SystemController 构造器执行...");
    }

    /**
     * SmartInitializingSingleton 接口中的 afterSingletonsInstantiated() 方法将会在所有的非惰性单实例 Bean 初始化完成之后进行回调。
     */
    @Override
    public void afterSingletonsInstantiated() {
        LOG.info("实例初始化完成...");
    }

    /**
     * http://localhost:8080/sys/get
     *
     * @return
     */
    @GetMapping("get")
    public String getTest() {
        JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
        ObjectNode objectNode = nodeFactory.objectNode();
        objectNode.put("dateTime", new Date().toString());
        objectNode.put("id", id++);
        objectNode.put("count", count++);
        System.out.println(objectNode.toString());
        return objectNode.toString();
    }


}